﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLKDS.DTO;

namespace QLKDS.DAL
{
    public class AuthorManager
    {
        public AuthorsInfo GetAuthorsById(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var queryAuthors = from d in db.Authors
                                   where d.Status.Equals(1)
                                    where d.Id.Equals(id)
                                   select new AuthorsInfo { Id = d.Id, Name = d.Name, Description = d.Description, CreatedBy = d.CreatedBy, CreatedDate = d.CreatedDate, ModifiedBy = d.ModifiedBy, ModifiedDate = d.ModifiedDate };

                return queryAuthors.FirstOrDefault();
            }
        
        }


        public IList<AuthorsInfo> ListAuthor()
        {
            using (var db = new QLKDSDataContext())
            {
                var queryAuthors = from d in db.Authors
                                   where d.Status.Equals(1)
                                   select new AuthorsInfo { Id = d.Id,Name = d.Name, Description = d.Description, CreatedBy = d.CreatedBy, CreatedDate = d.CreatedDate, ModifiedBy = d.ModifiedBy, ModifiedDate = d.ModifiedDate};

                return queryAuthors.ToList();
            }
        }
    

        public void CreateAuthors(Author Authors)
        {
            using (var db = new QLKDSDataContext())
            {
                db.Authors.InsertOnSubmit(Authors);

                db.SubmitChanges();
               
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Authors"></param>
        public void UpdateAuthors(Author Authors)
        {
            using (var db = new QLKDSDataContext())
            {
                var _Authors = db.Authors.Single(d => d.Id.Equals(Authors.Id));

                _Authors.Name = Authors.Name;
                _Authors.Description = Authors.Description;
                _Authors.CreatedBy = Authors.CreatedBy;
                _Authors.CreatedDate = Authors.CreatedDate;
                _Authors.ModifiedBy = Authors.ModifiedBy;
                _Authors.ModifiedDate = Authors.ModifiedDate;
                db.SubmitChanges();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public void DeleteAuthors(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var _Authors = db.Authors.Single(d => d.Id.Equals(id));

                db.Authors.DeleteOnSubmit(_Authors);
                db.SubmitChanges();
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool HasNameExisted(string name)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.Authors
                            where d.Name.Equals(name)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="code"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool HasNameDuplicated(string name, long id)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.Authors
                            where d.Name.Equals(name)
                            where !d.Id.Equals(id)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }

     
    }
}
